<template>
  <div>
     <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>商品管理</el-breadcrumb-item>
        <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
        <el-row :gutter='10'>
            <el-col :span='8'>
                <el-input v-model="queryData.query" clearable @clear='getGoods' placeholder="请输入内容">
                <el-button  @click="getGoods" slot="append" icon="el-icon-search"></el-button>
                </el-input>
            </el-col>
            <el-col :span='5'>
                <el-button @click="addGoods" type='primary'>添加商品</el-button>
            </el-col>
        </el-row>
        <!-- 商品列表 -->
        <el-table :data='goodsList' border stripe>
            <el-table-column type='index'></el-table-column>
            <el-table-column label='商品名称' prop='goods_name'></el-table-column>
            <el-table-column label='商品价格' width='80' prop='goods_price'></el-table-column>
            <el-table-column label='商品重量' width='80' prop='goods_weight'></el-table-column>
            <el-table-column label='创建时间' width='160'>
                <template slot-scope="scope">
                    {{ scope.row.add_time | formData }}
                </template>
            </el-table-column>
            <el-table-column label='操作' width='130'>
                <template slot-scope="scope">
                    <el-button type="primary" size='mini' icon="el-icon-edit"></el-button>
                    <el-button @click="removeGoods(scope.row.goods_id)" type="danger" size='mini' icon="el-icon-delete"></el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryData.pagenum"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="queryData.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
        </el-pagination>
    </el-card>

    
  </div>
</template>

<script>
export default {
  data () {
    return {
        queryData:{
            query:'',
            pagenum:1,
            pagesize:10
        },
        goodsList:[],
        total:0
    }
  },

  methods: {
      async getGoods(){
          const res=await this.axios.get('goods',{
              params:this.queryData
          });
          if(res.meta.status==200){
              this.goodsList=res.data.goods;
              this.total=res.data.total;
          }
        //   console.log(this.goodsList);
        //   console.log(this.total);
      },
    //   处理每页条数
    handleSizeChange(val){
        this.queryData.pagesize=val;
        this.getGoods();
    },
    // 当前页码发生变化
    handleCurrentChange(val){
         this.queryData.pagenum=val;
         this.getGoods();
    },
    // 删除商品
    removeGoods(id){
        this.$cfm('此操作将永久删除该商品, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async () => {
         const res=await this.axios.delete(`goods/${id}`);
         if(res.meta.status==200){
             this.$mg.success('删除商品成功!');
             this.getGoods();
         }
        }).catch(() => {
          this.$mg.info('已取消删除')         
        });
    },
    // 添加商品
    addGoods(){
        this.$router.push('/add');
    }
  },

  created () {
      this.getGoods();
  }
}
</script>

<style lang='less' scoped>
// .el-button{
//     margin-right: 5px;
// }
</style>
